Add MCViNE sample kernels as Union processes (and standalone samples) - #2716
Conversation
Add the MCViNE entry to enum process, a pointer_to_a_MCViNE_physics_storage_struct member to union data_transfer_union, and the two dispatch cases in physics_my and physics_scattering (guarded by PROCESS_MCVINE_DETECTOR). All MCViNE kernel processes share this one type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUUwywmxwaAvj7JoVQJ9y2
mcvine-lib: ports of the MCViNE sample kernels (mccomponents/lib/kernels/sample and mcvine/acc SANS2D_ongrid), a run-time expression evaluator, table and grid readers, DOS / Debye-Waller helpers, MCViNE IDF phonon readers, sample shapes and the MCViNE HomogeneousNeutronScatterer transport. mcvine-union-lib: glue between the kernels and the Union framework. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUUwywmxwaAvj7JoVQJ9y2
MCViNE_<kernel>_process (Union) and MCViNE_<kernel> (standalone) for ConstantEnergyTransfer, ConstantQE, ConstantvQE, E_Q, Broadened_E_Q, LorentzianBroadened_E_Q, E_vQ, SQ, SvQ, SQE, DGSSXRes, SANS2D_ongrid, Phonon_IncoherentElastic, Phonon_IncoherentInelastic, Phonon_CoherentInelastic_PolyXtal and Phonon_CoherentInelastic_SingleXtal. Documentation in contrib/doc/MCViNE_kernels.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUUwywmxwaAvj7JoVQJ9y2
One Tests_samples/Test_MCViNE_<kernel> instrument per kernel, running the Union process (comp_select=1) or the standalone component (comp_select=2) on the same sample, with an %Example line for each. Test inputs in data/MCViNE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUUwywmxwaAvj7JoVQJ9y2
|
@Fahima-Islam this looks absolutely great, good job! :-) The compilation issue for mcstas-antlr is intermittent and will be fixed via an incoming PR. |
|
Wow, great job! Really like how the code is organized such that duplication is avoided across the stand alone samples and Union processes. This offers a lot of exciting new capabilities! The run times are also faster than I feared for the advanced inelastic capabilities. You mention that duplicated efforts such as 2D grid (Q,E) are not ported, and of course its extra work to do that, but in general we welcome having several independent ways to calculate the same thing for a good cross check. |
|
@Fahima-Islam absolutely nice stuff indeed. :-) Just a word of warning - the Windows platform will require a bit more work it seems, but Claude might help you with that. (Pick the right artefact from https://github.com/mccode-dev/McCode/actions/runs/36691645550?pr=2716 and investigate the failing new MCVINE instrument Most often the issues are along the lines of
|
|
@Fahima-Islam I have dug out the Windows results for you It looks like a number of them indeed are things like replacements of I will boot up my Windows later to see if I can get a hold of some of this... |
rpcndr.h (pulled in by windows.h) has '#define small char', so 'int i, small;' in mcvine_S_Phonon_IncoherentInelastic became 'int i, char;' under MSVC and broke the build of every instrument that includes mcvine-lib.c. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
@willend thanks a lot for digging out the Windows results! All 16 5f6df3c renames it to Let's see what the Windows CI says now. |
|
@Fahima-Islam great! |
|
Seems to do he trick indeed @Fahima-Islam - snipped from the Windows compile log: FYI we are also in the midst of restructuring parts of |
|
@willend thanks for confirming! All CI has now finished:
On the Union restructuring: no problem with waiting. Once |
|
@mads-bertelsen thank you! I'm glad the code structure and the run times work for you. Good point about independent implementations as cross-checks. I left those kernels out to keep this PR focused, but they would be straightforward to add in a separate follow-up PR using the same This kind of work has also become much faster with AI-assisted ("vibe") coding: this PR, porting 16 kernels from MCViNE's C++, the Union glue and the test instruments, came together far quicker than it would have by hand. Most of my time went into checking the physics and validating against MCViNE and the references. So a follow-up with the overlapping kernels is a realistic next step rather than a long project, and having two independent implementations side by side is exactly the kind of cross-check that makes the fast route trustworthy. I'd start that after this PR is merged and the Union restructuring has settled, so it is built on the new Union interface. Does that sound good? |
|
@Fahima-Islam I've recently seen intermittent failures on As a side-note both of I think I will simply add an issue for that to later perform a separate investigation. |
|
@Fahima-Islam #2712 is merged to main. I have instructed my local Claude to update this PR with the required changes. |
Main now has the self-contained Union library (mccode-dev#2712): the Union types moved from union-lib.c into union-lib.h, and union-suffix.c is gone, since each process registers its own dispatch case. Conflicts resolved: - union-lib.c: keep main's version. The MCViNE entry in enum process and the pointer_to_a_MCViNE_physics_storage_struct member of data_transfer_union move to union-lib.h, where those types now live. - union-suffix.c: deleted, as on main. The MCViNE dispatch cases it held are registered by the MCViNE components in the following commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016QQzBnsrndv9v3cLryvVaR
mccode-dev#2712 made the Union components self-contained, so the MCViNE processes no longer rely on the code generator to load the Union library or on union-suffix.c for their dispatch: - Each MCViNE_*_process loads the library with %include "union-lib" instead of guarding on #ifndef Union, takes the shared state with union_acquire() in INITIALIZE, and gives it back with union_release() in a new FINALLY section. - mcvine_union_register() takes the process list to add to, and the processes pass &union_state_p->u_process_list instead of the removed g_process_list global. - union-lib.h gets empty default dispatch macros for MCViNE, and mcvine-union-lib.h, which every MCViNE process includes, replaces them with the two case MCViNE: entries. PROCESS_MCVINE_DETECTOR, which only guarded the union-suffix.c cases, is dropped. - Like the other Union components, the processes accept the deprecated, unused string init="" parameter. - The component headers, mcvine-union-lib.h and MCViNE_kernels.md describe the new registration instead of union-lib.c/union-suffix.c. The 16 Test_MCViNE_* instruments need no change. Run with comp_select=1 and 2 at a fixed seed, all 32 detector outputs are identical to the PR as submitted (classic mcstas with the former code generator injection), both with the current classic mcstas and with mccode-antlr 0.30.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016QQzBnsrndv9v3cLryvVaR
|
|
Will investigate separately, likely fixed via incoming #2729 If this ends up as the only issue / error I plan to merge @Fahima-Islam. Changes will make it to |
|
All tests except (Investigating the unrelated |
@Fahima-Islam that sounds great, and the work done to validate is great, that makes it a far more professional contribution than what “vibe coding” can sound like. |
|
@willend @mads-bertelsen thank you both! @willend thanks a lot for adapting the PR to the new self-contained Union scheme yourself, and for checking that all 32 test outputs stayed identical, with mccode-antlr as well. That saved me a round trip, and I'm glad the MCViNE kernels will make it into v3.8.9. Thanks also for taking ILL_H5 to a separate issue. @mads-bertelsen thank you for the kind words about the validation. That was where most of the effort went, so it's great to hear it came across. I'd like to start the follow-up PR with the overlapping kernels (isotropic, gridded S(Q,E), powder, single-crystal, SANS spheres and multiphonon), built on the new Union interface from current |







Free-form text area
This PR adds 16 sample scattering kernels from MCViNE (https://github.com/mcvine/mcvine,
mccomponents/lib/kernels/sample, plusSANS2D\_ongridfrom https://github.com/mcvine/acc). McStas has no equivalent for any of them. Each kernel is available in two forms:contrib/MCViNE\_<kernel>\_process.comp(geometry, containers, absorption and multiple scattering by Union), andcontrib/MCViNE\_<kernel>.comp(box / (hollow) cylinder / sphere, same transport as MCViNE's HomogeneousNeutronScatterer).Both forms call the same kernel code in
share/mcvine-lib.c.Kernels added
Isotropic\_Sqw-format grid, optional final-energy focusingMCViNE takes functions such as E(Q) and S(Q) as strings evaluated at run time (fparser). The same is supported here through a small built-in expression evaluator, e.g.
E\_Q="20\*sin(Q\*1.5)^2".Kernels not added, because McStas already covers them: isotropic scattering (
Incoherent), gridded S(Q,E) (Isotropic\_Sqw), powder and single-crystal diffraction (PowderN,Single\_crystal), SANS spheres, and multiphonon (IncoherentPhonon\_process, NCrystal). MCViNE's EPSC diffraction kernel is unfinished upstream and is not ported.Runtime-library change (Union core, 12 added lines). Union dispatches physics with a
switchonenum process. This PR registers one new process type,MCViNE:share/union-lib.c: adds an enum entry and adata\_transfer\_unionmembershare/union-suffix.c: adds twocase MCViNE:blocks guarded byPROCESS\_MCVINE\_DETECTOR, the same pattern as the other processesAll 16 kernels share this type. Its storage struct holds pointers to the kernel and to its sampling function, so further MCViNE kernels need no more core changes. Existing processes are untouched. The processes are
NOACC(CPU only).New files
share/mcvine-lib.h/.c– kernel physics, expression evaluator, table/grid readers (files found throughOpen\_File), DOS and Debye–Waller helpers, MCViNE IDF phonon readers, shapes and standalone transportshare/mcvine-union-lib.h/.c– Union gluecontrib/MCViNE\_\*\_process.comp(16),contrib/MCViNE\_\*.comp(16)contrib/doc/MCViNE\_kernels.md– overview, input formats, differences from MCViNE, validationexamples/Tests\_samples/Test\_MCViNE\_\*/(16 test instruments)data/MCViNE/– small test inputs (toy fcc phonon IDF set and atoms file, Debye DOS, S(q,w) grid, SANS map; ~0.6 MB)Where the port differs from MCViNE (documented in
contrib/doc/MCViNE\_kernels.md):unbiased=1option: available on four kernels whose MCViNE sampling loops are slightly biased; the default reproduces MCViNE.Tests
Test\_MCViNE\_<kernel>instrument runs the same sample as the Union process (comp\_select=1) or as the standalone component (comp\_select=2), with the same geometry, absorption and multiple scattering. It has an%Exampleline for each form.mctestresults: all 32%Examplecases pass inmctest --no-mpiat 100 % of their recorded values. The Union and standalone values of each kernel agree within about 2σ (0.3–1 % for most kernels; SQE, SANS2D and SingleXtal have larger statistical errors).Physics validation (scripts attached): analytic integrals, McStas
Incoherent, and independent Python calculations. Examples:Incoherentwithin 0.3 % for single scattering, and an analog MC within 0.2 % for double scattering.Documentation:
mcdocrenders the parameter tables of all 32 components.Formatting:
mccode-clangformathas been applied.Linting: the shared library is clean under
gcc -std=c99 -Wall -Wextraandcppcheck --enable=warning,portability,performance.---
Declaration of use of AI-tools
Please add a checkmark here if you used AI-tools during the work for this contribution
Furter, please describe how / where and for what the tools were used:
Claude (Anthropic, Claude Code) was used. It compared the MCViNE and McStas code bases to find the missing kernels, ported the MCViNE C++ kernels to C (
share/mcvine-lib.c), and wrote the Union glue and the 12-line Union core registration.I reviewed the ported physics against the MCViNE sources and the validation results.
---
Development OS / boundary conditions
Developed and tested on Linux (Ubuntu 24.04, gcc 13, x86_64) with McStas built from
main@ d3ab8cf, usingmcrun/mctestfrom the same build without MPI. No extra dependencies are needed. Two test instruments are slow at the default 1e6 neutrons in Union mode:Test\_MCViNE\_Phonon\_CoherentInelastic\_SingleXtal(about 150 s) andTest\_MCViNE\_E\_vQ(about 90 s).---
PR Checklist for contributing to McStas/McXtrace
My contribution includes a new component file
sigma\_\*,Vc,p\_transmit,target\_index/focus\_r, Unionpacking\_factor/interact\_fraction)mcdocutility and rendered a reasonable documentation page for the component (screenshots attached)mccode-clangformattool to apply the standard McCode component indentation schemecontribcomponent categoryMy contribution includes a new instrument file
mcdocutility and rendered a reasonable documentation page for the instrument%Example:line to describe expected behaviour (two per instrument)mcrun --c-lint"linter" and followed advice to remove most / all warnings that are raised (the new library code was linted with cppcheck and is clean)exampleshierarchy (examples/Tests\_samples/Test\_MCViNE\_\*/)data/MCViNE/folder.My work touches / adds to the runtime lib code (.c,.h etc in multiple locations
MCViNEenum entry, union member and two dispatch cases🤖 Generated with Claude Code